# Maintainer: Anatol Pomozov <anatol.pomozov@gmail.com>
# Contributor: Gordian Edenhofer <gordian.edenhofer@gmail.com>
# Contributor: Troy Engel <troyengel+arch@gmail.com>

pkgbase=bcc
pkgname=('bcc' 'bcc-tools' 'python-bcc')
pkgver=0.27.0
pkgrel=2
pkgdesc='BPF Compiler Collection'
arch=('loong64' 'x86_64')
url='https://github.com/iovisor/bcc'
license=('Apache')
makedepends=('cmake' 'clang' 'llvm' 'flex' 'bison' 'python' 'python-setuptools')
checkdepends=('netperf' 'iperf')
source=("bcc-$pkgver.tar.gz"::"https://github.com/iovisor/bcc/releases/download/v$pkgver/bcc-src-with-submodule.tar.gz")
sha512sums=('cf339db1b026d4183ff863188fd7c4a3b6b6c9ef56b42469a8c38bf91b0a9684b21e01fe304a88530626527f4b9f2d44c0e6f30af4152147f5a0fb72125610e3')

build() {
  mkdir -p bcc/build
  cd bcc/build
  export CFLAGS+=" -ffat-lto-objects"
  export CXXFLAGS+=" -ffat-lto-objects"

  # The python version is irrelevant at this stage
  cmake -DREVISION=$pkgver \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_INSTALL_LIBDIR=/usr/lib .. \
    -DENABLE_TESTS=OFF -DRUN_LUA_TESTS=OFF \
    -DENABLE_LLVM_SHARED=1   # FS#71163

  make
}

package_bcc() {
  pkgdesc='BPF Compiler Collection - C library and examples'
  depends=('clang' 'libelf')
  optdepends=('linux-headers: build modules against the Arch kernel'
    'linux-lts-headers: build modules against the LTS kernel'
    'linux-zen-headers: build modules against the ZEN kernel'
    'linux-hardened-headers: build modules against the HARDENED kernel'
    'bcc-tools: Python utilites using the BCC library'
    'python-bcc: Python 3 bindings for BCC'
    'luajit: Lua bindings for BCC')
  provides=('bcc' 'libbcc')

  cd bcc/build

  # Install the kitchen sink
  make DESTDIR="$pkgdir" install

  # These go in a split package python*-bcc
  rm -rf "$pkgdir"/usr/lib/python*

  # These go in a split package bcc-tools
  rm -rf "$pkgdir"/usr/share/bcc/{tools,man}
}

package_bcc-tools() {
  pkgdesc='BPF Compiler Collection - Tools'
  depends=('bcc' 'libedit' 'ethtool' 'python-bcc')

  cd bcc/build

  make -C tools DESTDIR="$pkgdir" install
  make -C man DESTDIR="$pkgdir" install
}

package_python-bcc() {
  pkgdesc='BPF Compiler Collection - Python 3 bindings'
  depends=('bcc' 'python')
  optdepends=(
    'python-netaddr: Network address representation and manipulation'
    'python-pyroute2: Netlink and Linux network configuration'
  )

  cd bcc/build

  # Force a quick python3 binding build (C library is already built)
  cmake -DREVISION=$pkgver \
    -DPYTHON_CMD="python" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_INSTALL_LIBDIR=/usr/lib ..
  make -C "src/python"

  # Install just those bindings
  make -C "src/python" DESTDIR="$pkgdir" install
}
